﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmtestlist : Form
    {
        private int cid = 0;
        private DataSet cds;
        private DataSet ds;
        private Form fa;
        int age = 0;
        string sex = "";
        public frmtestlist(int mcid)
        {
            cid = mcid;
            InitializeComponent();
        }
        public frmtestlist()
        {           
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddldate.Text == "之间")
            {
                dtps.Visible = true;
                dtpe.Visible = true;
            }
            else if (ddldate.Text == "所有")
            {
                dtps.Visible = false;
                dtpe.Visible = false;
            }
            else
            {
                dtps.Visible = true;
                dtpe.Visible = false;
            }
        }

        private void frmtestlist_Load(object sender, EventArgs e)
        {
            ddldate.Text = "所有";
            dtps.Value = DateTime.Now;
            dtpe.Value = DateTime.Now;
            if (cid != 0)
            {
                string sql = "select cname,csex,cbirthday from customer where cid=" + cid;
                cds = DbHelperOleDb.Query(sql);
                if (cds.Tables[0].Rows.Count > 0)
                {
                    bnc.Visible = false;
                    
                    this.Text = cds.Tables[0].Rows[0][0].ToString() + cds.Tables[0].Rows[0][1].ToString();
                    try
                    {
                        DateTime dt = DateTime.Parse(cds.Tables[0].Rows[0][2].ToString());
                        age = (DateTime.Now.Year - dt.Year);
                        sex = cds.Tables[0].Rows[0][1].ToString();
                        this.Text += age + "岁";
                    }
                    catch (Exception ex)
                    {
                        
                        
                    }
                    bind();
                }
            }
            else//按顺序浏览数据
            {
                cds = DbHelperOleDb.Query("select cid,cname,csex,cbirthday from customer");
                if (cds.Tables[0].Rows.Count > 0)
                {
                    //cid = int.Parse(cds.Tables[0].Rows[0][0].ToString());
                    //bind();
                    bsc.DataSource = cds;
                    bnc.BindingSource = bsc;
                }
            }           
        }

        private void bind()
        {
            dgvtest.AutoGenerateColumns = false;
           
            string sql;
            sql = "select [CID],[MDate],[MWeight],[MBMI],[MFatAll],[MFatUp],[MMetaAll],[MMeatUp],[MThews],[MThewType],[MWater],[MAge],[MFatIn],[MBone],[MXW],[MUBelly],[MDBelly],[Mwaistline],[MHip],[MRleg],[MRARM],[MRSlet],[MReaction],[mid] from measureInfo where cid=" + cid;
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and mdate>#{0}#",dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and mdate>#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "小于")
            {
                sql += string.Format(" and mdate<#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "等于")
            {
                sql += string.Format(" and mdate=#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "之间")
            {
                sql += string.Format(" and mdate>#{0}# and mdate<#{1}#", dtps.Value.ToShortDateString(),dtpe.Value.ToShortDateString());
            }
            sql += " order by mdate desc";
            ds = DbHelperOleDb.Query(sql);
            dgvtest.DataSource = ds.Tables[0];
            int widths = 0;
            for (int i = 0; i < dgvtest.Columns.Count; i++)
            {
                dgvtest.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCellsExceptHeader);  // 自动调整列宽  
                widths += dgvtest.Columns[i].Width;   // 计算调整列后单元列的宽度和                       
            }
            if (widths >= dgvtest.Size.Width)  // 如果调整列的宽度大于设定列宽  
                dgvtest.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;  // 调整列的模式 自动  
            else
                dgvtest.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;  // 如果小于 则填充  
            
            
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bind();
        }

        private void bsc_CurrentChanged(object sender, EventArgs e)
        {
            DataSet ds = bsc.DataSource as DataSet;
            cid = int.Parse(ds.Tables[0].Rows[bsc.Position]["cid"].ToString());
            this.Text = ds.Tables[0].Rows[bsc.Position][1].ToString() + ds.Tables[0].Rows[bsc.Position][2].ToString();
            try
            {
                DateTime dt = DateTime.Parse(ds.Tables[0].Rows[bsc.Position][3].ToString());
                age = (DateTime.Now.Year - dt.Year);
                sex = cds.Tables[0].Rows[bsc.Position][1].ToString();
                this.Text += (DateTime.Now.Year - dt.Year) + "岁";
            }
            catch (Exception ex)
            {


            } 
            bind();

        }

        private void btnreport_Click(object sender, EventArgs e)
        {
            string id = ds.Tables[0].Rows[dgvtest.CurrentRow.Index]["cid"].ToString();
            frmreports f = new frmreports(id);
            f.Show();
        }

        private void dgvtest_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvtest.Columns[e.ColumnIndex].Name == "grpt")
            {
                //占击按钮操作
                int id=int.Parse(ds.Tables[0].Rows[dgvtest.CurrentRow.Index]["mid"].ToString());
                frmreport f = new frmreport(id);
                f.Show();
            }
            if (dgvtest.Columns[e.ColumnIndex].Name == "gpic")
            {
                //占击按钮操作
                int id = int.Parse(ds.Tables[0].Rows[dgvtest.CurrentRow.Index]["mid"].ToString());
                frmpic f = new frmpic(id);
                f.Show();
            }
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            frmtest f = new frmtest(cid);
            //f.MdiParent = this.MdiParent;
            //fa=this.MdiParent;
            //this.MdiParent = null;
            f.ShowDialog();
            bind();
            //this.MdiParent = fa;
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            int i = 0,id=0;
            i = dgvtest.CurrentRow.Index;
            id = int.Parse(ds.Tables[0].Rows[i]["mid"].ToString());
            if (MessageBox.Show("你真的要删除" + DateTime.Parse(ds.Tables[0].Rows[i][1].ToString()).ToLongDateString() + "的记录吗？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string sql = "delete from  measureInfo where mid=" + id;
                DbHelperOleDb.ExecuteSql(sql);
                bind();
            }

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int i = 0, id = 0;
            i = dgvtest.CurrentRow.Index;
            id = int.Parse(ds.Tables[0].Rows[i]["mid"].ToString());
            frmtest f = new frmtest(-1,id);
            f.ShowDialog();
            bind();

        }

        private void dgvtest_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            //if (e.RowIndex >= dgvtest.Rows.Count - 1)
            //    return;
            DataGridViewRow dgr = dgvtest.Rows[e.RowIndex];
            try
            {
                float BMI = 0f,zf=0f;
                BMI = float.Parse(dgr.Cells[2].Value.ToString());
                zf = float.Parse(dgr.Cells[3].Value.ToString());
                dgr.Cells[2].Style.BackColor = myfun.checkBMI(BMI);
                dgr.Cells[3].Style.BackColor = myfun.checkzf(sex,age,zf);
                    //dgr.DefaultCellStyle.ForeColor = Color.Red;
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 

        }

        private void btnpint_Click(object sender, EventArgs e)
        {
            myprint m = new myprint(dgvtest);
            m.Print();
        }
    }
}
